package com.itcm.skymail.product.controller;

import com.itcm.common.utils.PageUtils;
import com.itcm.common.utils.R;
import com.itcm.skymail.product.entity.ProductAttrValueEntity;
import com.itcm.skymail.product.service.AttrService;
import com.itcm.skymail.product.vo.AttrRespVo;
import com.itcm.skymail.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author cm_fighting
 * @email cmtianxie@163.com
 * @date 2020-08-27 14:20:36
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 修改spu规格
     */
    @PostMapping("update/{spuId}")
    public R updateProductAttrValueBySpuId(@PathVariable("spuId") Long spuId,
                                           @RequestBody List<ProductAttrValueEntity> productAttrValueEntities) {
        attrService.updateProductAttrValueBySpuId(spuId, productAttrValueEntities);
        return R.ok();
    }

    /**
     * 获取spu规格
     */
    @GetMapping("base/listforspu/{spuId}")
    public R listForSpuBySpuId(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> list = attrService.listForSpuBySpuId(spuId);

        return R.ok().put("data", list);
    }

    /**
     * 获取分类规格参数
     */
    @GetMapping("{attrType}/list/{catelogId}")
    public R listByCatelogId(@PathVariable("attrType") String attrType,
                             @RequestParam Map<String, Object> params,
                             @PathVariable("catelogId") Long catelogId) {

        PageUtils page = attrService.queryPageByCatId(attrType, params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
//        AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attrRespVo = attrService.getDetailById(attrId);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo) {
//        attrService.save(attr);
        attrService.saveAttr(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo) {
        attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody List<Long> attrIds) {
//        attrService.removeByIds(Arrays.asList(attrIds));
        attrService.removeCasByIds(attrIds);
        return R.ok();
    }

}
